package com.yowasa.xiuxian.engine.battle.strategy.skill.water;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Lists;
import com.yowasa.xiuxian.annoations.BS;
import com.yowasa.xiuxian.constant.XiuxianConstants;
import com.yowasa.xiuxian.engine.battle.strategy.CalAbility;
import com.yowasa.xiuxian.engine.battle.strategy.InherentStrategy;
import com.yowasa.xiuxian.entity.bo.battle.BattleContent;
import com.yowasa.xiuxian.entity.bo.battle.BuffContent;
import com.yowasa.xiuxian.entity.bo.battle.UserBattleContent;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.ElementEnum;
import com.yowasa.xiuxian.entity.enums.InherentTiming;
import com.yowasa.xiuxian.entity.enums.StageEnum;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.BattleHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@BS(name = "清心咒", upAbility = 4, needTravel = 1000)
@Order(-1002)
@Component
public class QingXinZhou extends CalAbility implements InherentStrategy {
    @Autowired
    private BattleHelper battleHelper;

    @Override
    public ElementEnum getElement() {
        return ElementEnum.WATER;
    }

    @Override
    public void action(UserBattleContent first, UserBattleContent second, BattleContent content) {
        if (first.getUserBO().getUserConfig().getStudiedSpecialEffect().contains("净水之息")) {
            List<String> keys = Lists.newArrayList();
            for (Map.Entry<String, BuffContent> entry : first.getBuff().entrySet()) {
                if (!entry.getValue().isPositive()) {
                    keys.add(entry.getKey());
                }
            }
            for (String key : keys) {
                first.getBuff().remove(key);
            }
            if (keys.size() > 0) {
                content.getMsgLog().add(first.getUserBO().getName() + "因清心咒消除了自己所有的负面状态");
            }
        } else {
            String key = "";
            for (Map.Entry<String, BuffContent> entry : first.getBuff().entrySet()) {
                if (!entry.getValue().isPositive()) {
                    key = entry.getKey();
                    break;
                }
            }
            if (StringUtils.isNotBlank(key)) {
                BuffContent buffContent = first.getBuff().get(key);
                if (buffContent.getNum() > 1) {
                    buffContent.setNum(buffContent.getNum() - 1);
                } else {
                    first.getBuff().remove(key);
                }
                content.getMsgLog().add(first.getUserBO().getName() + "因清心咒消除了自己一层" + key);
            }
        }
    }

    @Override
    public boolean check(UserBattleContent user, BattleContent content) {
        for (BuffContent value : user.getBuff().values()) {
            if (!value.isPositive()) {
                return true;
            }
        }
        return false;
    }

    @Override
    public List<InherentTiming> getTiming() {
        return Lists.newArrayList(InherentTiming.SELF_BEGIN);
    }

    @Override
    public void limit(UserBO user) {
        if (user.getElement().get("水") < XiuxianConstants.DEFAULT_ELEMENT_ACTIVITY) {
            throw new HandlerException("学习/使用清心咒必须具有水灵根");
        }
        if (user.getStage() < StageEnum.ZHU_JI.getLevel()) {
            throw new HandlerException("学习/使用清心咒至少要始明之青");
        }
    }

    @Override
    public String limitDesc() {
        return "达到始明之青且具有水灵根";
    }

    @Override
    public String desc() {
        return "自己的回合开始时消除自身一层负面buff";
    }
}
